<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML lang=en xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
	<TITLE>DoctorDoc | Edit Data | EducationDegrees</TITLE>
	<META http-equiv=Content-Type content="text/html; charset=UTF-8" />
</HEAD>
<BODY>

<?php
include("nav.php");
?>

<h2>Edit Data | EducationDegrees</h2>

<form action="dbEduDe.php" method="post">
<table>
<tr><td>ID</td><td><input type="text" name="add_degree" /></td>
<td>Subject</td><td><input type="text" name="add_subject" /></td></tr>
<tr><td>University</td><td><input type="text" name="add_university" /></td>
<td>Location</td><td><input type="text" name="add_location" /></td></tr>
<tr><td>Thesis Title</td><td><input type="text" name="add_thesistitle" /></td>
<td>Start Date</td><td><input type="text" name="add_startdate" /></td></tr>
<tr><td>Supervisor</td><td><input type="text" name="add_supervisor" /></td>
<td>End Date</td><td><input type="text" name="add_enddate" /></td></tr>
<tr><td></td><td><input value="Submit" type="submit" /></td></tr>
</table>
</form>


<?php
$con = mysql_connect('drdoc2.cfcbem1draxy.us-east-1.rds.amazonaws.com:3306', 'DrDocRoot', 'DrDoc5m4ll');
if (!$con) {
	 die ('Error: Could not connect to mySQL database');
}
$mysql = "DrDocSmall";
if (!mysql_select_db($mysql)) {
	die ('Error: Could not connect to mySQL database');
}
echo "<br />\n";

/* INSERT VALUES INTO DATABASE */
if (count($_POST)!=0 && $_POST['add_degree']!="" ) { 
/* !!!!!!!!!!!!!!!!!!PROTOTYPE IS CURRENTLY USING DEGREE TO VALIDATE!!!!!!!!!!!!!!!!!!*/
$sql = "INSERT INTO EducationDegrees (Degree, Subject, University, Location, ThesisTitle, StartDate,  EndDate, Supervisor)
VALUES
('$_POST[add_degree]', '$_POST[add_subject]', '$_POST[add_university]', '$_POST[add_location]','$_POST[add_thesistitle]','$_POST[add_startdate]', '$_POST[add_enddate]','$_POST[add_supervisor]')";

if (!mysql_query($sql,$con))
  {
  die('Error: There is an error');
  }
else echo "Data saved\n";
}
else echo "<br />\n";


//http://stackoverflow.com/questions/68651/can-i-get-php-to-stop-replacing-characters-in-get-or-post-arrays
/* DELETE VALUES FROM DATABASE */
if (count($_POST)!=0) {
/* !!!!!!!!!!!!!!!!!!PROTOTYPE IS CURRENTLY USING DEGREE TO VALIDATE!!!!!!!!!!!!!!!!!!*/
/* !!!!!!!!!!!!!!!!!!ACTUAL WORKING PAGE SHOULD USE ID # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
while (list($key,$value) = each($_POST))
{
	if (substr($key,0,7)=='delete_' && $value=='Yes') 
	{
		mysql_query("DELETE FROM EducationDegrees WHERE Degree='" . substr($key,7) . "'");
	}
}
}


/* GET INFO FROM DATABASE */
echo "<form action='dbEduDe.php' method='post'>\n";
echo "<table border='1' style='border-collapse:collapse'>\n";
echo "<tr><th>ID</th><th>Subject</th><th>University</th><th>Location</th><th>Thesis Title</th><th>Start Date</th><th>End Date</th><th>Supervisor</th><th>Delete</th></tr>\n";
$sql = "SELECT * FROM EducationDegrees";
$result = mysql_query($sql, $con);
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>";
    echo "<td>" . $row['Degree'] . "</td>";
    echo "<td>" . $row['Subject'] . "</td>";
    echo "<td>" . $row['University'] . "</td>";
    echo "<td>" . $row['Location'] . "</td>";
    echo "<td>" . $row['ThesisTitle'] . "</td>";
    echo "<td>" . $row['StartDate'] . "</td>";
    echo "<td>" . $row['EndDate'] . "</td>";
    echo "<td>" . $row['Supervisor'] . "</td>";
/* !!!!!!!!!!!!!!!!!!PROTOTYPE IS CURRENTLY USING DEGREE TO VALIDATE!!!!!!!!!!!!!!!!!!*/
/* !!!!!!!!!!!!!!!!!!ACTUAL WORKING PAGE SHOULD USE ID # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
    echo "<td>" . "<input type='checkbox' name='delete_" . $row['Degree'] . "' value='Yes' />" . "</td>\n";
    echo "</tr>";
}
echo "</table>\n";
echo "<input type='submit' name='DeleteSelected' value='Delete Selected' />\n";
echo "</form>\n";

/* CLOSE CONNECTION */
mysql_free_result($result);
mysql_close($con);
?>
</BODY>
</HTML>
